package com.xijia.example.manage.controller;

import com.xijia.example.manage.model.Address;
import com.xijia.example.result.Result;
import com.xijia.example.util.IPUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/api")
public class ApiController {

    /**
     * 通过 ip 获取 ip 的地址信息
     * @param ip
     * @return
     */
    @PostMapping("/getAddressByIp")
    public Result<Address> getAddressByIp(@RequestParam String ip) {
        try {
            return Result.success(IPUtil.getAddressByIp(ip.trim()));
        } catch (Exception e) {
            return Result.error(500, "获取地址信息失败");
        }
    }

    /**
     * 获取当前用户的ip
     * @return
     */
    @PostMapping("/getIp")
    public Result<String> getIp(HttpServletRequest request) {
        return Result.success(IPUtil.getIp(request));
    }
}

